package com.zl.oauth.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

/**
 * 必须设置，否则code授权码模式无法使用。设置之后根据access_token访问受保护资源，认证会出问题。跟ResourceServer有冲突
 */
@Configuration
@EnableWebSecurity
//@Order(150)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
//    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.httpBasic().and().authorizeRequests()//httpBasic()不能省略，否则授权码模式不能生成确认授权页面
//                .antMatchers("/oauth/**").permitAll()
//                .antMatchers("/auth/**").authenticated()
                .anyRequest().permitAll()
                .and().csrf().disable();
    }

//    @Bean
    @Override
    protected UserDetailsService userDetailsService(){
        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
        manager.createUser(User.withUsername("zhangli").password("123456").authorities("USER").build());
        return manager;
    }
}